<?php
/**
 * 商家统计
 * User: Administrator
 * Date: 2018/10/17
 * Time: 14:42
 */

namespace app\merchant\controller;
use think\Db;
class Meter  extends Init
{
    function index(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            $sj = input('sj')?input('sj'):'';

            $where['o.store_id'] = $_SESSION['think']['admin_user']['store_id'];
            if(is_numeric(input('materiel_id')) === true){
                $where['o.materiel_id'] = input('materiel_id');
                $materielId = Db::table('je_order_goods')->alias('g')->join('je_goods o','g.goods_id = o.goods_id')->field('g.order_id,o.materiel_id')->where($where)->select();
                $m = array_column($materielId,'order_id');
                unset($where['o.materiel_id']);
                $where['o.order_id'] = array('in',$m);
            }


            if(!empty($sj)){
                $sj = explode(' - ',$sj);
                $where['o.add_time'] = array(array('egt',strtotime($sj[0])),array('elt',strtotime($sj[1]))) ;
            }
            $where['u.name|o.order_sn'] = array('like','%'.trim($key).'%');

            $where['o.order_status'] = 3;
            $list = Db::table('je_order')->alias('o')->join('je_user u','o.user_id = u.user_id')->field('o.order_id,o.order_sn,o.pay_price,o.type,o.end_time,o.confirm_time,o.add_time,u.name as sname')->order('order_id desc')->where($where)->paginate(input('limit'))->toArray();

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }

        $materiel = db('materiel')->where('parent_id=0')->field('materiel_id,name,parent_id')->select();
        foreach ($materiel as $key=>$itme){
            $materiel[$key]['mat'] = db('materiel')->where("parent_id = {$itme['materiel_id']}")->field('materiel_id,name,parent_id')->select();
        }

        return view('list',['list'=>$materiel]);
    }
}